//执行定义变量来表示不同的合约
const UniswapV2Router02  = artifacts.require('./UniswapV2Router02.sol');
const WETH9  = artifacts.require('./test/WETH9.sol');
const ERC20  = artifacts.require('./test/ERC20.sol');
const config = require('../migrations/constans');
// 测试命令:truffle test
//执行定义合约中的其他操作
contract('swap',accounts => {

    var router_address = config.router_address;

    let  tokenA = config.tokenA;
    let  tokenB = config.tokenB;

    it('get account pre successfully', async () => {
        ERC20_ONE = await ERC20.at(tokenA);
        ERC20_TWO = await ERC20.at(tokenB);
        let one_balanceOf =  (await ERC20_ONE.balanceOf.call(accounts[0])).toNumber();
        let two_balanceOf =  (await ERC20_TWO.balanceOf.call(accounts[0])).toNumber();
        console.log('token to token前余额TOKENA:',one_balanceOf,accounts[0]);
        console.log('token to token前余额TOKENB',two_balanceOf,accounts[0]);

        let fee_one_balanceOf =  (await ERC20_ONE.balanceOf.call(accounts[2])).toNumber();
        let fee_two_balanceOf =  (await ERC20_TWO.balanceOf.call(accounts[2])).toNumber();
        console.log('收取账户TOKENA前余额:',fee_one_balanceOf,accounts[2]);
        console.log('收取账户TOKENB前余额:',fee_two_balanceOf,accounts[2]);
    });


    it('new router deploy successfully', async () => {
        let  new_router_contract = await UniswapV2Router02.deployed();
        let factory_address = await new_router_contract.factory();
        router_address =     new_router_contract.address;
        console.log('新部署的router 合约',router_address,factory_address);

        ERC20_ONE = await ERC20.at(tokenA);
        ERC20_TWO = await ERC20.at(tokenB);
        //授权账户
        await ERC20_ONE.approve(router_address,1000000000);
        await ERC20_TWO.approve(router_address,1000000000);
        // console.log('分别向 router 合约授权 TOKENA TOKENB','1000000000');
    });


    it('swapExactTokensForTokens successfully', async () => {
        let  UniswapV2Router02_contract = await UniswapV2Router02.at(router_address);
        // let amountIn = 10000;
        // let amountOutMin = 9000;
        // let path = [tokenA,tokenB];
        // let to = '0xc783df8a850f42e7f7e57013759c285caa701eb6';
        // let deadline = 1639637649000;  
        // await UniswapV2Router02_contract.swapExactTokensForTokens(amountIn,amountOutMin,path,to,deadline);

        
        let amountOut = 10000;
        let amountInMax = 9999;
        let path = [tokenB,tokenA];
        let to = '0xc783df8a850f42e7f7e57013759c285caa701eb6';
        let deadline = 1639637649000;  
        await UniswapV2Router02_contract.swapTokensForExactTokens(amountOut,amountInMax,path,to,deadline);
    }); 


    it('get account pre successfully', async () => {
        ERC20_ONE = await ERC20.at(tokenA);
        ERC20_TWO = await ERC20.at(tokenB);
        let one_balanceOf =  (await ERC20_ONE.balanceOf.call(accounts[0])).toNumber();
        let two_balanceOf =  (await ERC20_TWO.balanceOf.call(accounts[0])).toNumber();
        console.log('token to token后余额TOKENA:',one_balanceOf,accounts[0]);
        console.log('token to token后余额TOKENB',two_balanceOf,accounts[0]);

        let fee_one_balanceOf =  (await ERC20_ONE.balanceOf.call(accounts[2])).toNumber();
        let fee_two_balanceOf =  (await ERC20_TWO.balanceOf.call(accounts[2])).toNumber();
        console.log('收取账户TOKENA后余额:',fee_one_balanceOf,accounts[2]);
        console.log('收取账户TOKENB后余额:',fee_two_balanceOf,accounts[2]);
    });

});

